package user

import (
	"context"
	model0 "gitee.com/liuxuezhan/ar-platform/assets-rpc/internal/dao/schema"
	"gitee.com/liuxuezhan/ar-platform/assets-rpc/internal/dao/schema/user"
	"gitee.com/liuxuezhan/ar-platform/assets-rpc/pkg/xerrors"
	"gitee.com/liuxuezhan/ar-platform/assets-rpc/rpc"
	"github.com/jinzhu/copier"
	"github.com/zeromicro/go-zero/core/logx"
	"gorm.io/gorm"
)

type PartnerRewardsModel struct {
	ctx context.Context
	db  *gorm.DB
	logx.Logger
}

func (m *PartnerRewardsModel) GetDB() *gorm.DB {
	return m.db
}

func NewPartnerRewardsModel(ctx context.Context, db *gorm.DB) *PartnerRewardsModel {
	return &PartnerRewardsModel{
		ctx:    ctx,
		db:     db,
		Logger: logx.WithContext(ctx),
	}
}

func (m *PartnerRewardsModel) PartnerRewardsPage(in *rpc.PartnerRewardsPageRequest) (*rpc.PartnerRewardsPageResponse, error) {
	var userPartnerRewards []*user.UserPartnerReward
	offset := in.Page.PageSize * (in.Page.PageNo - 1)

	// 查询所有用户银行信息
	query := m.db.Model(user.UserPartnerReward{})
	if in.Param.UserId != 0 {
		query = query.Where("user_id = ?", in.Param.UserId)
	}
	if in.Param.SubordinateId != 0 {
		query = query.Where("subordinate_id = ?", in.Param.SubordinateId)
	}
	if in.Param.RegisterTimeStart != 0 {
		query = query.Where("register_time > ? and register_time < ?", in.Param.RegisterTimeStart, in.Param.RegisterTimeEnd)
	}
	if in.Param.RechargeTimeStart != 0 {
		query = query.Where("recharge_time >? and recharge_time <?", in.Param.RechargeTimeStart, in.Param.RechargeTimeEnd)
	}
	query = query.Limit(int(in.Page.PageSize)).Offset(int(offset))
	tx := query.Find(&userPartnerRewards)
	if tx.Error != nil {
		return nil, xerrors.ErrDataConvert
	}
	var total int64
	_ = query.Count(&total).Error
	var partnerRewardsInfo []*rpc.PartnerRewardsInfo
	_ = copier.Copy(&partnerRewardsInfo, userPartnerRewards)

	return &rpc.PartnerRewardsPageResponse{
		DataList: partnerRewardsInfo,
		Total:    total,
	}, nil
}

func (m *PartnerRewardsModel) UserPartnerRewardsDel(in *rpc.UserPartnerDelRequest) (*rpc.UserPartnerDelResponse, error) {
	tx := m.db.Where("id = ? and user_id = ?", in.Id, in.UserId).Delete(&user.UserPartnerReward{
		Model: model0.Model{
			ID: (in.Id),
		},
		UserId: in.UserId,
	})
	if tx.Error != nil || tx.RowsAffected == 0 {
		return nil, xerrors.ErrDel
	}

	return &rpc.UserPartnerDelResponse{
		IsDel: true,
	}, nil
}

func (m *PartnerRewardsModel) UserPartnerRewardsInsert(in *rpc.UserPartnerInsertRequest) (*rpc.UserPartnerInsertResponse, error) {

	tx := m.db.Create(&user.UserPartnerReward{
		UserId:         in.UserId,
		InviteCode:     in.InviteCode,
		SubordinateID:  in.SubordinateId,
		RechargeAmount: in.RechargeAmount,
		RewardAmount:   in.RewardAmount,
		RechargeTime:   in.RechargeTime,
		RegisterTime:   in.RegisterTime,
	})
	if tx.Error != nil || tx.RowsAffected == 0 {
		return nil, xerrors.ErrInsert
	}

	return &rpc.UserPartnerInsertResponse{
		IsInsert: true,
	}, nil
}

// ------------------user partner_reward ------------------
func (m *PartnerRewardsModel) UserPartnerRewards(in *rpc.PartnerRewardsInfoRequest) (*rpc.PartnerRewardsInfoResponse, error) {
	var partnerRewards []*user.UserPartnerReward
	var partnerRewardList []*rpc.PartnerRewardsInfo

	// 查询所有用户银行信息
	db := m.db.Model(user.UserPartnerReward{})
	if in.UserId != 0 {
		db = db.Where("user_id = ?", in.UserId)
	}
	if in.SubordinateId != 0 {
		db = db.Where("subordinate_id = ?", in.SubordinateId)
	}

	db.Find(&partnerRewards)
	for _, partnerReward := range partnerRewards {

		userEWalletInfo := &rpc.PartnerRewardsInfo{
			UserId:         partnerReward.UserId,
			InviteCode:     partnerReward.InviteCode,
			SubordinateId:  partnerReward.SubordinateID,
			RechargeAmount: partnerReward.RechargeAmount,
			RewardAmount:   partnerReward.RewardAmount,
			RechargeTime:   partnerReward.RechargeTime,
			RegisterTime:   partnerReward.RegisterTime,
		}
		partnerRewardList = append(partnerRewardList, userEWalletInfo)
	}

	return &rpc.PartnerRewardsInfoResponse{
		PartnerRewardsInfoList: partnerRewardList,
	}, nil
}

func (m *PartnerRewardsModel) UserPartnerRewardsUpdate(in *rpc.UserPartnerUpdateRequest) (*rpc.UserPartnerUpdateResponse, error) {

	tx := m.db.Where("id = ?", in.Id).Updates(&user.UserPartnerReward{
		UserId:         in.UserId,
		InviteCode:     in.InviteCode,
		SubordinateID:  in.SubordinateId,
		RechargeAmount: in.RechargeAmount,
		RewardAmount:   in.RewardAmount,
		RechargeTime:   in.RechargeTime,
		RegisterTime:   in.RegisterTime,
	})
	if tx.Error != nil || tx.RowsAffected == 0 {
		return nil, xerrors.ErrUpdate
	}

	return &rpc.UserPartnerUpdateResponse{
		IsUpdate: true,
	}, nil
}
